<?php 
require_once '/function/db_conn.php';
?>
<div id="global">
	<div>
		<table>
			<tr>
				<td>
					<?php 
						$result = mysql_query('SELECT product_buy_id, title, b.image as image, winner, username FROM  product_buy b, member m 
							WHERE b.member_id = m.member_id AND status="F" AND winner <> "NULL" ORDER BY b.creation_time DESC LIMIT 2');
					?>
					<div id="tblLatestDeal-border">
						<h1 class="title" style="margin-bottom: 10px;"><span>最新成交</span></h1>
						<div class="global-content">
							<table id="tblLatestBuyer">
								<?php 
								while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
									$mbmResult = mysql_query('SELECT * FROM member WHERE member_id="' . $row["winner"] . '"');
									$mbmRow = mysql_fetch_array($mbmResult, MYSQL_ASSOC);
									$pbResult = mysql_query('SELECT count(*) as counter FROM product_buy WHERE product_buy_id="' . $row["product_buy_id"] . '"');
									$pbRow = mysql_fetch_array($pbResult, MYSQL_ASSOC);
								?>
								<tr>	
									<td colspan="2">
										<b>
											<a href="/product/sellerbid/pid/<?php echo $row["product_buy_id"];?>">
												<?php echo $row["title"];?>
											</a>
										</b>
									</td>
								</tr>
								<tr>
									<td>
										<?php 
										$pos = strrpos($row["image"], "http");
										if ($pos === false) {
											list($width, $height, $type, $attr) = getimagesize("./././" . $row["image"]);
										}
										else{
											list($width, $height, $type, $attr) = getimagesize($row["image"]);
										}
										$newWidth = 0;
										$newHeight = 0;
										if($width >= $height){
											$newWidth = 68;
											$newHeight = $height * 68 / $width;
										}
										else if($height > $width){
											$newHeight = 68;
											$newWidth = $width * 68 / $height;
										}
										?>
										<img src="<?php echo $row["image"];?>" width="<?php echo $newWidth;?>px" height="<?php echo $newHeight;?>px"></img>
									</td>
									<td>
										<a href="/member/profile/mid/<?php echo $mbmRow["member_id"];?>"><?php echo $mbmRow["username"];?></a>成功擊敗<?php echo $pbRow["counter"]?>位對手，奪得了<a href="/member/profile/mid/<?php echo $row["member_id"];?>"><?php echo $row["username"];?></a>的招標
									</td>
								</tr>
								<?php 
								}
								?>
							</table>
						</div>
					</div>
				</td>
				<td>
					<div id="banner">
						<img src="/images/layout/banner.jpg" width="640px" height="200px"/>
					</div>
				</td>
				<td style="vertical-align: top;">
					<OBJECT classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width="150"
				        height="100" codebase='http://www.apple.com/qtactivex/qtplugin.cab'>
				        <param name='src' value="your_mov_file.mov">
				        <param name='autoplay' value="false">
				        <param name='controller' value="true">
				        <param name='loop' value="false">
				        <EMBED src="/storage/demo.mov" width="180" height="120" autoplay="false" 
				        controller="true" loop="false" pluginspage='http://www.apple.com/quicktime/download/' scale="tofit">
				        </EMBED>
			        </OBJECT>
				</td>
			</tr>
		</table>
		<div class="global-border" style="padding: 0px;">
			<table>
				<tr>
					<td style="vertical-align: top;">
						<h1><span>助你一臂</span></h1>
						<table id="tblFindBuyer">
							<tr>
								<td>
									<table>
										<?php 
										$pb_mapper = new Application_Model_ProductBuyMapper();
										for($i=0; $i<sizeof($this->mbmEntries); $i++){
											$discount = 0;
											$pbEntries = $pb_mapper->findByDiscountMemberId($this->mbmEntries[$i]->member_id);
											for($j=0; $j<sizeof($pbEntries); $j++){
												if($discount + $pbEntries[$j]->final_price / $pbEntries[$j]->price == 1){
													$discount = $discount / ($j + 1);
												}
												else{
													$discount = ($discount + $pbEntries[$j]->final_price / $pbEntries[$j]->price) / ($j + 1);
												}
											}
											if($i % 2 == 0){
										?>
										<tr>
											<td>
												<table>
													<tr>
														<td rowspan="4" style="width:70px; padding: 0px;">
															<?php 
															$pos = strrpos($this->mbmEntries[$i]->image, "http");
															if ($pos === false) {
																list($width, $height, $type, $attr) = getimagesize("./././" . $this->mbmEntries[$i]->image);
															}
															else{
																list($width, $height, $type, $attr) = getimagesize($this->mbmEntries[$i]->image);
															}
															$newWidth = 0;
															$newHeight = 0;
															if($width >= $height){
																$newWidth = 68;
																$newHeight = $height * 68 / $width;
															}
															else if($height > $width){
																$newHeight = 68;
																$newWidth = $width * 68 / $height;
															}
															?>
															<img src="<?php echo $this->mbmEntries[$i]->image;?>" width="<?php echo $newWidth;?>px" height="<?php echo $newHeight;?>px"></img>
														</td>
														<td style="width: 130px;">
															<b>
																<a href='<?php echo $this->escape($this->action) . "/mid/" . $this->mbmEntries[$i]->member_id; ?>'>
																	<?php echo $this->mbmEntries[$i]->username;?>
																</a>
															</b>
														</td>
														<td rowspan="4" class="credit">
																<b><?php echo $this->mbmEntries[$i]->deal_credit;?></b>
																<br/>
																<div style="margin-top: 13px;color: #BBBBBB; font-size: 12px;">信用值</div>
														</td>
													</tr>
													<tr>
														<td><?php echo $this->discount . "<span style='color:red;'>" . round($discount * 100) . "% Off</span>";?></td>
													</tr>
													<tr>
														<td>
															<?php 
															echo $this->deal;
															$pbWinnerEntries = $pb_mapper->findByWinnerId($this->mbmEntries[$i]->member_id); 
															if(sizeof($pbWinnerEntries) > 0){
															?>
															<a href='<?php echo $this->escape($this->productAction) . "/pid/" . $pbWinnerEntries[0]->product_buy_id;?>' >
																<?php 
																if(strlen($pbWinnerEntries[0]->title) > 6){
																	echo substr($pbWinnerEntries[0]->title,0,6) . "...";
																}
																else{
																	echo $pbWinnerEntries[0]->title;
																}
																?>
															</a>
															<?php 
															}
															else{
																echo "沒有交易";
															}
															?>
														</td>
													</tr>
													<tr>
														<td colspan="2">
															<?php echo $this->escape($this->country);?>
															<?php 
															if($this->mbmEntries[$i]->travel != NULL){?>
															<img class="flag" src="/images/flag/<?php echo strtolower($this->mbmEntries[$i]->travel);?>.png"/>
															<?php 
															}
															else{
																echo ": 未選擇";
															}?>
														</td>
													</tr>
													<tr>
														<td colspan="3" style="padding-top:3px; color: #AAAAAA">
															<?php echo $this->mbmEntries[$i]->description;?>
														</td>
													</tr>
												</table>
											</td>
										</tr>
										<?php			
											}
										}
										?>
									</table>
								</td>
								<td>
									<table>
										<?php 
										$pb_mapper = new Application_Model_ProductBuyMapper();
										for($i=0; $i<sizeof($this->mbmEntries); $i++){
											$discount = 0;
											$pbEntries = $pb_mapper->findByDiscountMemberId($this->mbmEntries[$i]->member_id);
											for($j=0; $j<sizeof($pbEntries); $j++){
												if($discount + $pbEntries[$j]->final_price / $pbEntries[$j]->price == 1){
													$discount = $discount / ($j + 1);
												}
												else{
													$discount = ($discount + $pbEntries[$j]->final_price / $pbEntries[$j]->price) / ($j + 1);
												}
											}
											if($i % 2 == 1){
										?>
										<tr>
											<td>
												<table>
													<tr>
														<td rowspan="4" style="width:70px; padding: 0px;">
															<?php 
															$pos = strrpos($this->mbmEntries[$i]->image, "http");
															if ($pos === false) {
																list($width, $height, $type, $attr) = getimagesize("./././" . $this->mbmEntries[$i]->image);
															}
															else{
																list($width, $height, $type, $attr) = getimagesize($this->mbmEntries[$i]->image);
															}
															$newWidth = 0;
															$newHeight = 0;
															if($width >= $height){
																$newWidth = 68;
																$newHeight = $height * 68 / $width;
															}
															else if($height > $width){
																$newHeight = 68;
																$newWidth = $width * 68 / $height;
															}
															?>
															<img src="<?php echo $this->mbmEntries[$i]->image;?>" width="<?php echo $newWidth;?>px" height="<?php echo $newHeight;?>px"></img>
														</td>
														<td style="width: 130px;">
															<b>
																<a href='<?php echo $this->escape($this->action) . "/mid/" . $this->mbmEntries[$i]->member_id; ?>'>
																	<?php echo $this->mbmEntries[$i]->username;?>
																</a>
															</b>
														</td>
														<td rowspan="4" class="credit">
																<b><?php echo $this->mbmEntries[$i]->deal_credit;?></b>
																<br/>
																<div style="margin-top: 13px;color: #BBBBBB; font-size: 12px;">信用值</div>
														</td>
													</tr>
													<tr>
														<td><?php echo $this->discount . "<span style='color:red;'>" . round($discount * 100) . "% Off</span>";?></td>
													</tr>
													<tr>
														<td>
															<?php 
															echo $this->deal;
															$pbWinnerEntries = $pb_mapper->findByWinnerId($this->mbmEntries[$i]->member_id); 
															if(sizeof($pbWinnerEntries) > 0){
															?>
															<a href='<?php echo $this->escape($this->productAction) . "/pid/" . $pbWinnerEntries[0]->product_buy_id;?>' >
																<?php 
																if(strlen($pbWinnerEntries[0]->title) > 6){
																	echo substr($pbWinnerEntries[0]->title,0,6) . "...";
																}
																else{
																	echo $pbWinnerEntries[0]->title;
																}
																?>
															</a>
															<?php 
															}
															else{
																echo "沒有交易";
															}
															?>
														</td>
													</tr>
													<tr>
														<td colspan="2">
															<?php echo $this->escape($this->country);?>
															<?php 
															if($this->mbmEntries[$i]->travel != NULL){?>
															<img class="flag" src="/images/flag/<?php echo strtolower($this->mbmEntries[$i]->travel);?>.png"/>
															<?php 
															}
															else{
																echo ": 未選擇";
															}?>
														</td>
													</tr>
													<tr>
														<td colspan="3" style="padding-top:3px; color: #AAAAAA">
															<?php echo $this->mbmEntries[$i]->description;?>
														</td>
													</tr>
												</table>
											</td>
										</tr>
										<?php			
											}
										}
										?>
									</table>
								</td>
							</tr>
						</table>
					</td>
					<td style="vertical-align: top;">
						<h1 class="title"><span>最新求助</span></h1>
						<div class="global-content">
							<table id="tblLatestBuyer">
								<?php 
								$result = mysql_query('SELECT product_buy_id, pb.image as image, title, country, price, day, username, mbm.member_id as member_id, sub_image1, sub_image2
									FROM product_buy pb, member mbm WHERE pb.member_id = mbm.member_id ORDER BY pb.creation_time DESC LIMIT 10');
								$i=0;
								while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
									if($i % 2 == 0){
										echo "<tr><td>";
									}
									else{
										echo "<td>";
									}
								?>
								<table>
									<tr>
										<td style="text-align:center;">
											<b>
												<a href="/product/sellerbid/pid/<?php echo $row["product_buy_id"];?>">
													<?php echo $row["title"];?>
												</a>
											</b>
										</td>
									</tr>
									<tr>
										<td>
											<table style="width: 100%;">
												<tr>
													<td rowspan="2" style="width: 100px; text-align: right;">
														<?php 
														$pos = strrpos($row["image"], "http");
														if ($pos === false) {
															list($width, $height, $type, $attr) = getimagesize("./././" . $row["image"]);
														}
														else{
															list($width, $height, $type, $attr) = getimagesize($row["image"]);
														}
														$newWidth = 0;
														$newHeight = 0;
														if($width >= $height){
															$newWidth = 75;
															$newHeight = $height * 75 / $width;
														}
														else if($height > $width){
															$newHeight = 75;
															$newWidth = $width * 75 / $height;
														}
														?>
														<img src="<?php echo $row["sub_image1"];?>" width="<?php echo $newWidth;?>px" height="<?php echo $newHeight;?>px"></img>
													</td>
														<?php 
														$pos = strrpos($row["image"], "http");
														if ($pos === false) {
															list($width, $height, $type, $attr) = getimagesize("./././" . $row["sub_image1"]);
														}
														else{
															list($width, $height, $type, $attr) = getimagesize($row["sub_image1"]);
														}
														$newWidth = 0;
														$newHeight = 0;
														if($width >= $height){
															$newWidth = 37;
															$newHeight = $height * 37 / $width;
														}
														else if($height > $width){
															$newHeight = 37;
															$newWidth = $width * 37 / $height;
														}
														?>
													<td><img src="<?php echo $row["sub_image1"];?>" width="<?php echo $newWidth;?>px" height="<?php echo $newHeight;?>px"></img></td>
												</tr>
												<tr>
														<?php 
														$pos = strrpos($row["sub_image2"], "http");
														if ($pos === false) {
															list($width, $height, $type, $attr) = getimagesize("./././" . $row["sub_image2"]);
														}
														else{
															list($width, $height, $type, $attr) = getimagesize($row["sub_image2"]);
														}
														$newWidth = 0;
														$newHeight = 0;
														if($width >= $height){
															$newWidth = 37;
															$newHeight = $height * 37 / $width;
														}
														else if($height > $width){
															$newHeight = 37;
															$newWidth = $width * 37 / $height;
														}
														?>
													<td><img src="<?php echo $row["sub_image2"];?>" width="<?php echo $newWidth;?>px" height="<?php echo $newHeight;?>px"></img></td>
												</tr>
											</table>
										</td>
									</tr>
									<tr>	
										<td>
											<table>
												<tr>
													<td style="text-align:center; width: 102px;"><b>限價: <span style="color: red;">HKD<?php echo $row["price"];?></span></b></td>
													<td style="text-align:center; width: 66px;"><b>限期: <span style="color: red;"><?php echo $row["day"];?>天</span></b></td>
												</tr>
												<tr>
													<td colspan="2" style="text-align:center;">
														用戶名:
														<a href="/member/profile/mid/<?php echo $row["member_id"];?>">
															<?php echo $row["username"];?>
														</a>
													</td>
												</tr>
												<tr>
													<td colspan="2" style="text-align:center;">指定購買地&nbsp;<img class="flag" src="/images/flag/<?php echo strtolower($row["country"]);?>.png"/></td>
												</tr>
											</table>
										</td>
									</tr>
									<tr>	
										<td><hr></td>
									</tr>
								</table>
								<?php 
									if($i % 2 == 0){
										echo "</td>";
										if($i + 1 == sizeof($this->mbmEntries)){
											echo "<td></td></tr>";
										}
									}
									else{
										echo "</td></tr>";
									}
									$i++;
								}
								?>
							</table>
						</div>
					</td>
				</tr>
			</table>
		</div>
	</div>
</div>